'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0909
    Inherits Snac

    Public Sub New()
        MyBase.New(&H9, &H9)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function

    Private _ErrorCode As Integer
    Public Property ErrorCode() As Integer
        Get
            Return _ErrorCode
        End Get
        Set(ByVal value As Integer)
            _ErrorCode = value
        End Set
    End Property

    Private _ErrorSubCode As New TlvErrorSubCode
    Public ReadOnly Property ErrorSubCode() As TlvErrorSubCode
        Get
            Return _ErrorSubCode
        End Get
    End Property

    Private _ErrorDescriptionUrl As New TlvErrorDescriptionUrl
    Public ReadOnly Property ErrorDescriptionUrl() As TlvErrorDescriptionUrl
        Get
            Return _ErrorDescriptionUrl
        End Get
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        _ErrorCode = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H8
                    _ErrorSubCode.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H4
                    _ErrorDescriptionUrl.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        Me.SetTotalSize(index)
    End Sub
End Class

Public Class TlvErrorDescriptionUrl
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H6)
    End Sub

    Private _ErrorDescriptionUrl As String
    Public Property ErrorDescriptionUrl() As String
        Get
            Return _ErrorDescriptionUrl
        End Get
        Set(ByVal value As String)
            _ErrorDescriptionUrl = value
        End Set
    End Property

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(_ErrorDescriptionUrl))

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        _ErrorDescriptionUrl = ByteConverter.ToString(data.GetRange(4, DataSize))
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return _ErrorDescriptionUrl.Length
    End Function
End Class